import uix
import uiutil
import base
import uthread
import uicls
import uiconst
import blue
FRAME_COLOR = (1.0, 1.0, 1.0, 0.25)

class PushButtonGroup(uicls.Container):
    __guid__ = 'uicls.PushButtonGroup'
    __resPath__ = 'res:/UI/Component/newbutton.blue'
    default_align = uiconst.RELATIVE
    default_idx = 0

    def ApplyAttributes(self, attributes):
        uicls.Container.ApplyAttributes(self, attributes)
        self.sr.selected = uicls.Fill(parent=self, color=(0.0, 0.0, 0.0, 0.5), padLeft=-1, padRight=-1, state=uiconst.UI_HIDDEN)
        self.sr.hilite = uicls.Fill(parent=self, color=(1.0, 1.0, 1.0, 0.125), padLeft=-1, padRight=-1, state=uiconst.UI_HIDDEN)
        self.toggleEnabled = True



    def Startup(self, data):
        totalW = 0
        mainParent = uicls.Container(name='mainParent', parent=self, idx=0, left=8, width=8, top=2, height=4)
        self.sr.buttonParent = mainParent
        uicls.Line(parent=mainParent, align=uiconst.TOLEFT, color=(0.0, 0.0, 0.0, 0.1))
        uicls.Line(parent=mainParent, align=uiconst.TOLEFT, color=(1.0, 1.0, 1.0, 0.1))
        for (label, panel, parentClass, args,) in data:
            par = uicls.Container(name=(args[0] + 'Par'), parent=mainParent, align=uiconst.TOLEFT, state=uiconst.UI_NORMAL)
            par.OnMouseEnter = self.BtnMouseEnter
            par.OnMouseExit = self.BtnMouseExit
            par.OnMouseDown = self.BtnMouseDown
            par.OnMouseUp = self.BtnMouseUp
            par.OnClick = self.Select
            par.sr.args = args
            par.sr.parentClass = parentClass
            par.sr.panel = panel
            par.sr.panelName = label
            par.selected = 0
            t = uicls.Label(text=label, parent=par, left=1, top=1, fontsize=10, letterspace=1, uppercase=1, state=uiconst.UI_DISABLED, align=uiconst.CENTER)
            par.sr.label = t
            par.width = (t.textwidth + 12)
            totalW += (par.width + 2)
            uicls.Line(parent=mainParent, align=uiconst.TOLEFT, color=(0.0, 0.0, 0.0, 0.1))
            uicls.Line(parent=mainParent, align=uiconst.TOLEFT, color=(1.0, 1.0, 1.0, 0.1))
            self.height = max(self.height, min(32, (t.textheight + 9)))

        self.width = (totalW + 18)
        self.DeselectAll()



    def DeselectAll(self):
        for each in self.sr.buttonParent.children:
            if each.sr.label:
                self.Deselect(each)

        self.OnNoneSelected()



    def SelectPanelByName(self, panelName):
        for each in self.sr.buttonParent.children:
            if (each.sr.panelName == panelName):
                self.Select(each)




    def SelectPanelByArgs(self, args):
        for each in self.sr.buttonParent.children:
            if (each.sr.args == args):
                self.Select(each)




    def BtnMouseEnter(self, btn, *args):
        if not btn.selected:
            uiutil.Transplant(self.sr.hilite, btn)
            self.sr.hilite.state = uiconst.UI_DISABLED



    def BtnMouseExit(self, btn, *args):
        if (self.sr.hilite in btn.children):
            self.sr.hilite.state = uiconst.UI_HIDDEN
            uiutil.Transplant(self.sr.hilite, self)



    def BtnMouseDown(self, btn, *args):
        btn.sr.label.top = 2



    def BtnMouseUp(self, btn, *args):
        if not btn.selected:
            btn.sr.label.top = 1



    def GetSelected(self):
        for each in self.sr.buttonParent.children:
            if getattr(each, 'selected', False):
                return each.sr.args




    def Select(self, btn, *args):
        if (self.toggleEnabled and btn.selected):
            self.Deselect(btn)
            self.OnNoneSelected()
            return 
        btn.selected = 1
        btn.sr.label.top = 2
        if (self.sr.hilite in btn.children):
            self.sr.hilite.state = uiconst.UI_HIDDEN
            uiutil.Transplant(self.sr.hilite, self)
        uiutil.Transplant(self.sr.selected, btn)
        self.sr.selected.state = uiconst.UI_DISABLED
        for each in btn.parent.children:
            if (each.sr.label and (each != btn)):
                self.Deselect(each)

        if btn.sr.panel:
            btn.sr.panel.state = uiconst.UI_PICKCHILDREN
        pc = btn.sr.parentClass
        if hasattr(pc, 'LoadPushButtonPanel'):
            pc.LoadPushButtonPanel(btn.sr.args)



    def Deselect(self, btn, *args):
        btn.selected = 0
        btn.sr.label.top = 1
        if (self.sr.selected in btn.children):
            self.sr.selected.state = uiconst.UI_HIDDEN
            uiutil.Transplant(self.sr.selected, self)
        if btn.sr.panel:
            btn.sr.panel.state = uiconst.UI_HIDDEN
        if hasattr(btn.sr.parentClass, 'UnloadPushButtonPanel'):
            btn.sr.parentClass.UnloadPushButtonPanel(btn.sr.args)



    def OnNoneSelected(self, *args):
        return 




class FlatButtonGroup(PushButtonGroup):
    __guid__ = 'uicls.FlatButtonGroup'
    __resPath__ = None

    def ApplyAttributes(self, attributes):
        PushButtonGroup.ApplyAttributes(self, attributes)
        self.sr.selected.SetRGBA(1.0, 1.0, 1.0, 0.2)
        self.sr.hilite.SetRGBA(1.0, 1.0, 1.0, 0.125)



    def Startup(self, data, selectedArgs = None):
        self.width = 200
        self.numButtons = len(data)
        splitValue = (1.0 / len(data))
        totalW = 0
        mainParent = uicls.Container(name='mainParent', parent=self, idx=0, pos=(0, 4, 0, 4), align=uiconst.TOALL)
        self.sr.buttonParent = mainParent
        uicls.Frame(parent=mainParent, color=FRAME_COLOR)
        for (index, (label, panel, parentClass, args,),) in enumerate(data):
            par = uicls.Container(name=('buttonPar%d' % index), parent=mainParent, align=uiconst.TOLEFT, state=uiconst.UI_NORMAL)
            par.OnMouseEnter = self.BtnMouseEnter
            par.OnMouseExit = self.BtnMouseExit
            par.OnMouseDown = self.BtnMouseDown
            par.OnMouseUp = self.BtnMouseUp
            par.OnClick = self.ButtonSelect
            par.sr.args = args
            par.sr.parentClass = parentClass
            par.sr.panel = panel
            par.sr.panelName = label
            par.selected = 0
            par.sr.isDisabled = False
            t = uicls.Label(text=label, parent=par, left=1, top=1, fontsize=10, letterspace=1, uppercase=1, state=uiconst.UI_DISABLED, align=uiconst.CENTER)
            t.autoPos = uix.AUTOPOSCENTER
            par.sr.label = t
            par.width = (t.textwidth + 12)
            totalW += (par.width + 2)
            uicls.Line(parent=par, align=uiconst.TOLEFT, color=FRAME_COLOR, idx=0)
            self.height = max(self.height, min(32, (t.textheight + 9)))
            par.minHeight = min(32, (t.textheight + 9))
            par.splitValue = splitValue

        self.width = (totalW + 18)
        self._AdjustButtons()
        for item in mainParent.children:
            if item.sr.label:
                self.Deselect(item)

        if (selectedArgs is None):
            self.OnNoneSelected()
        else:
            self.SelectPanelByArgs(selectedArgs)



    def OnNoneSelected(self, *args):
        for item in self.sr.buttonParent.children:
            if item.name.endswith('Par'):
                self.Select(item)
                return 




    def _AdjustButtons(self):
        (pl, pt, pw, ph,) = self.sr.buttonParent.GetAbsolute()
        total = 0
        i = 0
        if (pw <= 0):
            return 
        for child in self.sr.buttonParent.children:
            sv = getattr(child, 'splitValue', None)
            if (sv is not None):
                (mw, mh,) = (None, None)
                if (child.align in (uiconst.UI_ALLEFT, uiconst.UI_ALRIGHT)):
                    i += 1
                    if (i == self.numButtons):
                        child.width = (pw - total)
                    else:
                        child.width = max(child.minHeight, int((sv * pw)))
                    total += child.width




    def _OnResize(self):
        if self.sr.buttonParent:
            self._AdjustButtons()



    def EnableButton(self, panelName):
        for button in self.sr.buttonParent.children:
            if (button.sr.panelName == panelName):
                button.sr.label.color.a = 1
                button.sr.isDisabled = False




    def DisableButton(self, panelName):
        for button in self.sr.buttonParent.children:
            if (button.sr.panelName == panelName):
                button.sr.label.color.a = 0.4
                button.sr.isDisabled = True




    def ButtonSelect(self, button, *args):
        if hasattr(button, 'sr'):
            if button.sr.isDisabled:
                eve.Message('SelectLocation', {'location': button.sr.panelName})
            elif (button.selected == 0):
                self.Select(button)
            button.sr.label.top = 1




class Button(uicls.ButtonCore):
    __guid__ = 'uicls.Button'
    default_alwaysLite = False
    default_iconSize = 32
    default_icon = None
    default_color = (1.0, 1.0, 1.0, 0.75)

    def ApplyAttributes(self, attributes):
        self.disabled = False
        self.alwaysLite = attributes.get('alwaysLite', self.default_alwaysLite)
        self.color = attributes.get('color', self.default_color)
        self.iconPath = attributes.get('icon', self.default_icon)
        self.iconSize = attributes.get('iconSize', self.default_iconSize)
        args = attributes.get('args', None)
        uicls.ButtonCore.ApplyAttributes(self, attributes)
        if (args == 'self'):
            self.args = self



    def _Initialize(self, *args, **kw):
        self.blinking = False
        uiutil.Merge(blue.os.LoadObject('res:/UI/Component/newbuttonUIFrame_2.blue'), self)
        uicls.ButtonCore._Initialize(self, *args, **kw)
        sm.GetService('window').TryLoadUIColors()



    def Prepare_(self):
        self.sr.label = uicls.Label(text='', parent=self, idx=0, top=0, state=uiconst.UI_DISABLED, fontsize=10, letterspace=1, uppercase=1, color=self.color)
        self.sr.label.autoPos = uix.AUTOPOSCENTER
        if (self.iconPath is not None):
            if self.iconSize:
                width = self.iconSize
                height = self.iconSize
            else:
                width = height = min(self.width, self.height)
            uicls.Icon(parent=self, state=uiconst.UI_DISABLED, align=uiconst.CENTER, pos=(0,
             0,
             width,
             height), icon=self.iconPath, ignoreSize=True, color=self.color)
            self.sr.label.state = uiconst.UI_HIDDEN
        self.sr.activeframe = uiutil.GetChild(self, 'activeline')
        self.sr.activeframe.state = uiconst.UI_HIDDEN
        self.sr.hilite = uiutil.GetChild(self, 'hilite')
        self.sr.hilite.state = uiconst.UI_HIDDEN
        self.LiteMode(self.alwaysLite)
        self.CheckIfDefault()
        uthread.new(sm.GetService('window').CheckControlAppearance, self)



    def Update_Size_(self):
        if (self.iconPath is None):
            self.width = min(256, (self.fixedwidth or max(48, (self.sr.label.width + 24))))
            lt = (max(2, (self.sr.label.textheight / 12)) - 2)
            self.sr.label.top = lt
            self.height = min(32, (self.sr.label.textheight + 9))



    def SetLabel_(self, label):
        if ((not self) or self.destroyed):
            return 
        text = self.text = label
        text = text.replace('<center>', '').replace('</center>', '')
        self.sr.label.text = text
        self.Update_Size_()



    def OnSetFocus(self, *args):
        if self.disabled:
            return 
        if (self and ((not self.destroyed) and (self.parent and (self.parent.name == 'inlines')))):
            if (self.parent.parent and self.parent.parent.sr.node):
                browser = uiutil.GetBrowser(self)
                if browser:
                    uthread.new(browser.ShowObject, self)
        if (self and ((not self.destroyed) and (self.sr and self.sr.activeframe))):
            self.sr.activeframe.state = uiconst.UI_DISABLED
        btns = self.GetDefaultBtnsInSameWnd()
        if btns:
            self.SetWndDefaultFrameState(btns, 0)



    def LiteMode(self, on = True):
        if on:
            uiutil.GetChild(self, 'shape').state = uiconst.UI_HIDDEN
            uiutil.GetChild(self, 'innerline').state = uiconst.UI_DISABLED
            uiutil.GetChild(self, 'hiliteLite').state = uiconst.UI_DISABLED
            uiutil.GetChild(self, 'hiliteNormal').state = uiconst.UI_HIDDEN
        elif (self.alwaysLite == False):
            uiutil.GetChild(self, 'shape').state = uiconst.UI_DISABLED
            uiutil.GetChild(self, 'innerline').state = uiconst.UI_HIDDEN
            uiutil.GetChild(self, 'hiliteLite').state = uiconst.UI_HIDDEN
            uiutil.GetChild(self, 'hiliteNormal').state = uiconst.UI_DISABLED



    def Disable(self):
        uicls.ButtonCore.Disable(self)
        self.state = uiconst.UI_NORMAL
        self.Hilite_(False)
        self.disabled = True



    def Enable(self):
        uicls.ButtonCore.Enable(self)
        self.disabled = False



    def _OnClick(self, *args):
        if ((not self) or self.destroyed):
            return 
        if self.disabled:
            return 
        uicls.ButtonCore._OnClick(self, *args)



    def _OnMouseDown(self, *args):
        if self.disabled:
            return 
        uicls.ButtonCore._OnMouseDown(self, args)



    def Hilite_(self, on):
        if self.disabled:
            return 
        uicls.ButtonCore.Hilite_(self, on)




class IconButton(uicls.Container):
    __guid__ = 'uicls.IconButton'
    default_alphaNormal = 0.6
    default_alphaOver = 1.0

    def ApplyAttributes(self, attributes):
        uicls.Container.ApplyAttributes(self, attributes)
        icon = attributes.icon
        iconAlign = attributes.iconAlign
        if (iconAlign is None):
            iconAlign = uiconst.TOALL
        self.func = attributes.func
        self.args = (attributes.args or ())
        ignoreSize = (attributes.ignoreSize or False)
        iconPos = (attributes.iconPos or (1, 2, 24, 24))
        self.alphaOver = (attributes.alphaOver or self.default_alphaOver)
        self.alphaNormal = (attributes.alphaOver or self.default_alphaNormal)
        self.state = (attributes.state or uiconst.UI_NORMAL)
        self.sr.icon = uicls.Icon(icon=icon, parent=self, pos=iconPos, align=iconAlign, idx=0, state=uiconst.UI_DISABLED, ignoreSize=ignoreSize)
        self.SetOpacity(self.default_alphaNormal)



    def OnClick(self, *args):
        if self.func:
            self.func(*self.args)



    def _OnMouseEnter(self, *args):
        if ((not self.dead) and getattr(self, 'alphaOver', None)):
            self.SetOpacity(self.alphaOver)



    def _OnMouseExit(self, *args):
        if ((not self.dead) and getattr(self, 'default_alphaNormal', None)):
            self.SetOpacity(self.default_alphaNormal)



    def ChangeIcon(self, *args, **kw):
        self.sr.icon.ChangeIcon(*args, **kw)




class BaseButton(uicls.Container):
    __guid__ = 'xtriui.BaseButton'

    def ApplyAttributes(self, attributes):
        uicls.Container.ApplyAttributes(self, attributes)



    def init(self):
        self.sr.preleft = None
        self.sr.pretop = None
        self.sr.selection = None
        self.sr.selected = 0
        self.sr.enterAlt = 0
        self.sr.hilite = None
        self.Click = None
        self.MouseEnter = None
        self.MouseExit = None
        self.DblClick = None
        self.enabled = 1
        self.blinking = 0
        self.clicks = 0



    def Select(self):
        if ((self is None) or self.destroyed):
            return 
        if (self.sr.selection is None):
            self.sr.selection = blue.os.LoadObject('res:/UI/Component/selection_glow.blue')
            self.children.append(self.sr.selection)
            self.sr.selection.align = uix.UI_ALCLIENT
            self.sr.selection.left = self.sr.selection.top = self.sr.selection.width = self.sr.selection.height = -int((self.width * 0.5))
        self.sr.selection.state = uix.UI_DISABLED
        self.sr.selected = 1



    def Deselect(self):
        if (self and (self.sr and (self.sr.selection is not None))):
            self.sr.selection.state = uix.UI_HIDDEN
            self.sr.selected = 0



    def Disable(self):
        self.color.a = 0.5
        self.enabled = 0



    def Enable(self):
        self.color.a = 1.0
        self.enabled = 1



    def _OnDblClick(self, *etc):
        if not self.enabled:
            return 
        if self.DblClick:
            self.clicks += 1
        elif self.Click:
            self.Click(self)



    def _OnClick(self, *etc):
        if not self.enabled:
            return 
        self.clicks += 1
        if self.DblClick:
            self.sr.clickTimer = base.AutoTimer(250, self.ClickTimer)
        elif self.Click:
            self.Click(self)



    def ClickTimer(self, *args):
        if (self.clicks == 1):
            if self.Click:
                self.Click(self)
        else:
            if (self.clicks >= 2):
                if self.DblClick:
                    self.DblClick(self)
        if not self.destroyed:
            self.clicks = 0
            self.sr.clickTimer = None



    def _OnMouseEnter(self, *etc):
        if not self.enabled:
            return 
        eve.Message('CCCellEnter')
        if getattr(self, 'over', None):
            if not getattr(self, 'active', 0):
                self.rectTop = self.over
        elif not self.sr.pretop:
            self.sr.pretop = self.top
            self.sr.preRectTop = self.rectTop
        self.rectTop += self.rectHeight
        self.top -= self.sr.enterAlt
        if self.MouseEnter:
            self.MouseEnter(self)



    def _OnMouseExit(self, *etc):
        if getattr(self, 'idle', None):
            if not getattr(self, 'active', 0):
                self.rectTop = self.idle
        elif (self.sr.pretop is not None):
            self.top = self.sr.pretop
            self.rectTop = self.sr.preRectTop
        if self.MouseExit:
            self.MouseExit(self)



    def _OnMouseDown(self, *args):
        if not self.enabled:
            return 
        self.top += self.sr.enterAlt



    def _OnMouseUp(self, *args):
        if not self.enabled:
            return 
        self.top -= self.sr.enterAlt




class BigButton(BaseButton):
    __guid__ = 'xtriui.BigButton'

    def Startup(self, width, height):
        self.setfocus = 0
        self.killfocus = 0
        self.blinking = 0
        self.width = width
        self.height = height
        self.sr.smallcaption = None
        self.sr.caption = None
        self.sr.hilite = uiutil.GetChild(self, 'hilite')
        self.sr.hilite.state = uix.UI_HIDDEN
        self.sr.shadow = uiutil.GetChild(self, 'shadow')
        self.sr.dot = self.children[1]
        self.sr.icon = uicls.Icon(uiutil.GetChild(self, 'icon'))
        self.sr.activeHilite = uiutil.GetChild(self, 'activeHilite')
        self.resetTop = None
        self.AdjustSizeAndPosition(width, height)



    def AdjustSizeAndPosition(self, width, height):
        shadow = self.sr.shadow
        shadow.left = -int((width * 0.19))
        shadow.top = -int((height * 0.095))
        shadow.width = ((abs(shadow.left) * 2) + width)
        shadow.height = ((abs(shadow.top) * 4) + height)
        hiliteOffset = max(-8, min(-6, (shadow.top + 6)))
        self.sr.hilite.left = self.sr.hilite.top = self.sr.hilite.width = self.sr.hilite.height = hiliteOffset
        self.sr.activeHilite.left = self.sr.activeHilite.top = self.sr.activeHilite.width = self.sr.activeHilite.height = hiliteOffset
        self.sr.dot.left = self.sr.dot.top = max(-1, -int((height * 0.0156)))
        self.sr.enterAlt = min(2, max(6, (self.height / 16)))



    def SetIconByIconID(self, iconID):
        if (iconID is not None):
            self.sr.icon.ChangeIcon(icon=iconID)



    def SetInCaption(self, capstr, atbottom = 0):
        if self.sr.caption:
            self.sr.caption.Close()
        if (('&' in capstr) and (';' in capstr)):
            capstr = self.ParseHTML(capstr)
        caption = uicls.Label(text=capstr, parent=self, top=self.height, fontsize=14, idx=0)
        if atbottom:
            caption.top = (self.height + 8)
        else:
            caption.top = (((self.height - caption.textheight) / 2) + 2)
        caption.left = (((self.width - caption.textwidth) / 2) + 1)
        caption.state = uix.UI_DISABLED
        self.sr.caption = caption



    def SetCaption(self, capstr, atbottom = 0):
        self.SetSmallCaption(capstr)



    def SetSmallCaption(self, capstr, inside = 0, maxWidth = None):
        if not self.sr.smallcaption:
            self.sr.smallcaption = uicls.Label(text='', parent=self, top=((6 + self.height) - ((self.height - 6) * inside)), state=uiconst.UI_DISABLED, idx=0)
        self.sr.smallcaption.busy = 1
        if (maxWidth is not None):
            self.sr.smallcaption.autowidth = 0
            self.sr.smallcaption.width = maxWidth
            self.sr.smallcaption.busy = 0
            self.sr.smallcaption.text = capstr
            self.sr.smallcaption.left = ((self.width - self.sr.smallcaption.width) / 2)
        else:
            self.sr.smallcaption.autowidth = 1
            self.sr.smallcaption.width = 0
            self.sr.smallcaption.busy = 0
            self.sr.smallcaption.text = capstr
            self.sr.smallcaption.left = ((self.width - self.sr.smallcaption.textwidth) / 2)



    def ParseHTML(self, text):
        for k in translatetbl:
            text = text.replace(k, translatetbl[k])

        return text



    def _OnMouseExit(self, *etc):
        if not self.blinking:
            self.sr.hilite.state = uix.UI_HIDDEN
        if self.MouseExit:
            self.MouseExit(self)
        self.timer = None
        if (self.resetTop is not None):
            self.top = self.resetTop
            self.resetTop = None



    def _OnMouseEnter(self, *etc):
        eve.Message('CCCellEnter')
        if not self.blinking:
            self.sr.hilite.state = uix.UI_DISABLED
        if self.MouseEnter:
            self.MouseEnter(self)
        self.timer = base.AutoTimer(500, self.ResetHilite)
        if (self.resetTop is not None):
            self.top += 1



    def ResetHilite(self, *args):
        if (eve.triapp.uilib.mouseOver != self):
            self.timer = None
            if not self.blinking:
                self.sr.hilite.state = uix.UI_HIDDEN



    def _OnMouseDown(self, *args):
        self.Blink(0)
        self.sr.hilite.state = uix.UI_HIDDEN
        if (self.resetTop is None):
            self.resetTop = self.top
            self.top = (self.top + self.sr.enterAlt)



    def _OnMouseUp(self, *args):
        if (eve.triapp.uilib.mouseOver == self):
            self.sr.hilite.state = uix.UI_DISABLED
        if (self.resetTop is not None):
            self.top = self.resetTop
            self.resetTop = None



    def Blink(self, on_off = 1, blinks = 3):
        if on_off:
            b = self.sr.hilite.children[0].children[0]
            b.tripass = b.tripass.CopyTo()
            for each in self.sr.hilite.children:
                for _each in each.children:
                    _each.tripass = b.tripass


            sm.GetService('ui').BlinkSpriteRGB(b, b.color.r, b.color.g, b.color.b, 750, blinks, passColor=1)
            self.sr.hilite.state = uix.UI_DISABLED
        else:
            self.sr.hilite.state = uix.UI_HIDDEN
            sm.GetService('ui').StopBlink(self.sr.hilite.children[0].children[0])
        self.blinking = on_off



    def OnSetFocus(self, *args):
        if self.setfocus:
            self.sr.activeHilite.state = uiconst.UI_DISABLED



    def OnKillFocus(self, *args):
        if self.killfocus:
            self.sr.activeHilite.state = uiconst.UI_HIDDEN




class ExpanderButton(uicls.Sprite):
    __guid__ = 'xtriui.ExpanderButton'

    def init(self):
        self.width = self.height = 11
        self.expanded = True



    def Startup(self, expanded, expandFunc, collapseFunc, state = uiconst.UI_NORMAL):
        self.state = state
        self.expandFunc = expandFunc
        self.collapseFunc = collapseFunc
        self.expanded = expanded
        self._UpdateIcon()



    def OnClick(self, *args):
        self.expanded = (not self.expanded)
        self._OnExpandedStateChanged()



    def _OnExpandedStateChanged(self, applyCallback = True):
        self._UpdateIcon()
        if not applyCallback:
            return 
        if self.expanded:
            if (self.collapseFunc is not None):
                self.collapseFunc()
        elif (self.expandFunc is not None):
            self.expandFunc()



    def SetState(self, expanded, applyCallback = True):
        self.expanded = (not expanded)
        self._OnExpandedStateChanged(applyCallback)



    def _UpdateIcon(self):
        if self.expanded:
            self.rectTop = 75
        else:
            self.rectTop = 64



translatetbl = {'&aring;': '\xe5',
 '&gt;': '>',
 '&yen;': '\xa5',
 '&ograve;': '\xd2',
 '&bull;': '\x95',
 '&trade;': '\x99',
 '&Ntilde;': '\xd1',
 '&Yacute;': '\xdd',
 '&Atilde;': '\xc3',
 '&aelig;': '\xc6',
 '&oelig;': '\x9c',
 '&auml;': '\xc4',
 '&Uuml;': '\xdc',
 '&Yuml;': '\x9f',
 '&lt;': '<',
 '&Icirc;': '\xce',
 '&shy;': '\xad',
 '&Oacute;': '\xd3',
 '&yacute;': '\xfd',
 '&acute;': '\xb4',
 '&atilde;': '\xc3',
 '&cedil;': '\xb8',
 '&Ecirc;': '\xca',
 '&not;': '\xac',
 '&AElig;': '\xc6',
 '&oslash;': '\xf8',
 '&iquest;': '\xbf',
 '&laquo;': '\xab',
 '&Igrave;': '\xcc',
 '&ccedil;': '\xc7',
 '&nbsp;': '\xa0',
 '&Auml;': '\xc4',
 '&brvbar;': '\xa6',
 '&Otilde;': '\xd5',
 '&szlig;': '\xdf',
 '&agrave;': '\xe0',
 '&Ocirc;': '\xd4',
 '&egrave;': '\xc8',
 '&iexcl;': '\xa1',
 '&frac12;': '\xbd',
 '&ordf;': '\xaa',
 '&ntilde;': '\xd1',
 '&ocirc;': '\xd4',
 '&Oslash;': '\xd8',
 '&THORN;': '\xde',
 '&yuml;': '\x9f',
 '&Eacute;': '\xc9',
 '&ecirc;': '\xca',
 '&times;': '\xd7',
 '&Aring;': '\xc5',
 '&tilde;': '~',
 '&mdash;': '-',
 '&Ugrave;': '\xd9',
 '&Agrave;': '\xc0',
 '&sup1;': '\xb9',
 '&eth;': '\xd0',
 '&iuml;': '\xcf',
 '&reg;': '\xae',
 '&Egrave;': '\xc8',
 '&divide;': '\xf7',
 '&Ouml;': '\xd6',
 '&igrave;': '\xcc',
 '&otilde;': '\xd5',
 '&pound;': '\xa3',
 '&frasl;': '/',
 '&ETH;': '\xd0',
 '&plusmn;': '\xb1',
 '&sup2;': '\xb2',
 '&frac34;': '\xbe',
 '&Aacute;': '\xc1',
 '&cent;': '\xa2',
 '&frac14;': '\xbc',
 '&euml;': '\xcb',
 '&iacute;': '\xcd',
 '&para;': '\xb6',
 '&ordm;': '\xba',
 '&uuml;': '\xdc',
 '&icirc;': '\xce',
 '&copy;': '\xa9',
 '&Iuml;': '\xcf',
 '&Ograve;': '\xd2',
 '&Ucirc;': '\xdb',
 '&Zeta;': 'Z',
 '&minus;': '-',
 '&deg;': '\xb0',
 '&and;': '&',
 '&curren;': '\xa4',
 '&ucirc;': '\xdb',
 '&ugrave;': '\xd9',
 '&sup3;': '\xb3',
 '&Acirc;': '\xc2',
 '&quot;': '"',
 '&Uacute;': '\xda',
 '&OElig;': '\x8c',
 '&uacute;': '\xda',
 '&acirc;': '\xc2',
 '&macr;': '\xaf',
 '&Euml;': '\xcb',
 '&Ccedil;': '\xc7',
 '&aacute;': '\xc1',
 '&micro;': '\xb5',
 '&eacute;': '\xc9',
 '&middot;': '\xb7',
 '&Iacute;': '\xcd',
 '&amp;': '&',
 '&uml;': '\xa8',
 '&thorn;': '\xde',
 '&ouml;': '\xd6',
 '&raquo;': '\xbb',
 '&sect;': '\xa7',
 '&oacute;': '\xd3'}

